import Vue from 'vue'
import http from "./http"
import qs from "qs"
import Util from './util'
Vue.prototype.$get = http.$get
Vue.prototype.$post = http.$post
Vue.prototype.$del = http.$del
Vue.prototype.$put = http.$put
Vue.prototype.$test = http.$test
Vue.prototype.$uploadFile = http.$uploadFile
Vue.prototype.$util = Util


// 替换路由
Vue.prototype._router = {
	push(path) {
		var url = '', query, animationType, animationDuration;
		if (typeof path == 'string') {
			url = path
		} else {
			url = path.path;
			query = qs.stringify(path.query);
			animationType = path.animationType
			animationDuration = path.animationDuration
		}
		uni.navigateTo({
			url: `${url}${url.includes('?') ? '&' : '?'}${query || ''}`,
			animationType,
			animationDuration
		});
	},
	replace(path) {
		var url = '', query, animationType, animationDuration;
		if (typeof path == 'string') {
			url = path
		} else {
			url = path.path || '';
			query = qs.stringify(path.query);
			animationType = path.animationType
			animationDuration = path.animationDuration
		}
		uni.redirectTo({
			url: `${url}${url.includes('?') ? '&' : '?'}${query || ''}`,
			animationType,
			animationDuration
		});
	}
}
// 解决uni-app slot 数组传递bug
Vue.prototype.$list = function (obj) {
	return Object.entries(obj).filter(item => item[0] != '_i').map(item => item[1])
}
// 返回
Vue.prototype.$back = (num = 1) => {
	uni.navigateBack(num)
}

// 提示框
Vue.prototype.$toast = (params) => {
	let data = {}
	if (typeof params == 'string') {
		data.title = params
	} else {
		data = params
	}

	Util.showToast(data)
}


// 获取文件
Vue.prototype.$getImage = (config) => {
	return new Promise((res, rec) => {
		uni.chooseImage({
			count: config.count || 9,
			success: (chooseImageRes) => {
				res(chooseImageRes)
			},
			fail: () => {
				rec()
			}
		});
	})
}
